<?php
require('inc/bootstrap.php');

// This file is exactly the same as download.php except it is used for online listening (via the HTML 5 audio element) and therefore does not report hits or downloads.

$currenturl = 'http';
if ($_SERVER["HTTPS"] == "on") {$currenturl .= "s";}
$currenturl .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$currenturl .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}else{
$currenturl .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
$url = parse_url($currenturl);
$urlpath = explode('/', $url['path']);
$ID = $urlpath[count($urlpath)-1];

if(!$ID)
{
	require('inc/header.php');
	echo '<h1>Oops!</h1><br />The file ID is missing from the page URL. Please correct this problem and then try again.';
	require('inc/footer.php');
	exit;
}
if ($_SERVER['HTTP_HOST'] == "$domain")
{
	// Send the end user to the correct server (multi-server setups only)
if ($multiserver == true)
{
pickhost();
	if ($server == '0')
	{
		// Pick a random node and see if the file exists on it.
global $ID;
		$mysqli = dbconnect();
		$query = "SELECT hostname FROM nodes WHERE active=1 ORDER BY rand()";
		$statement = $mysqli->stmt_init();
		$statement->prepare($query);
		$statement->execute();
		while($row = $statement->fetch_array())
		{
$hostname = 		$row['0'];
		  if (file_get_contents("http://$hostname.$domain/api/check.php?id=$ID") == "1")
		  {
			$server = $hostname;
			exit;
		  }
		}
		$statement->close();
		$mysqli->close();
	}
	header("Location: http://$server.$domain/dl/$ID");
	exit;
}
}
// If on a subdomain, just download the file directly.
if ($servername == $SQLMaster)
{
$mysqli = dbconnect();
$query = "SELECT filename,filesize FROM uploads WHERE id=?";
$statement = $mysqli->stmt_init();
$statement->prepare($query);
$statement->bind_param("i", $ID);
$statement->execute();
		$statement->store_result();
		$statement->bind_result($filename, $filesize);
		$statement->fetch();
$numrows = $statement->num_rows;
$statement->close();
$mysqli->close();
}
else
{
$filename = file_get_contents("http://$SQLMaster.$domain/api/filename.php?id=$ID");
}
if ($filename)
{
	if (file_exists($uploadsDir.$ID))
	{
		set_time_limit(0);
		header("Pragma: public"); // required
		header("Expires: 0"); 
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
		header("Cache-Control: private",false); // required for certain browsers 
		header('Content-Description: File Transfer');
		header('Content-Type: '.mime_content_type($uploadsDir.$ID));
		header('Content-Disposition: attachment; filename="'.$filename.'"');
		header('Content-Transfer-Encoding: binary');
		header('Content-Length: '.filesize($uploadsDir.$ID));
		flush();
		ob_flush();
		readfile($uploadsDir.$ID);
		exit;
	}
	else
	{
		// File does not exist, redirect to info page.
		header("Location: http://$domain/$ID");
		exit;
	}
}
else
{
  	require('inc/header.php');
	echo "<h1>Oops!</h1>
	We're sorry, but the file you are looking for doesn't exist. Please make sure its ID is correct and then try again. (404)";
	require('inc/footer.php');
  }
?>
